home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / SNIP0492.ARJ / ISQRT.C < prev    next >
C/C++ Source or Header  |  1989-08-04  |  2KB  |  83 lines

  1.  
  2. #define BITSPERLONG 32
  3.  
  4. #define TOP2BITS(x) ((x & (3 << (BITSPERLONG-2))) >> (BITSPERLONG-2))
  5.  
  6.  
  7. /* usqrt:
  8.     ENTRY x: unsigned long
  9.     EXIT  returns floor(sqrt(x) * pow(2, BITSPERLONG/2))
  10.  
  11.     Since the square root never uses more than half the bits
  12.     of the input, we use the other half of the bits to contain
  13.     extra bits of precision after the binary point.
  14.  
  15.     EXAMPLE
  16.         suppose BITSPERLONG = 32
  17.         then    usqrt(144) = 786432 = 12 * 65536
  18.                 usqrt(32) = 370727 = 5.66 * 65536
  19.  
  20.     NOTES
  21.         (1) change BITSPERLONG to BITSPERLONG/2 if you do not want
  22.             the answer scaled.  Indeed, if you want n bits of
  23.             precision after the binary point, use BITSPERLONG/2+n.
  24.             The code assumes that BITSPERLONG is even.
  25.         (2) This is really better off being written in assembly.
  26.             The line marked below is really a "arithmetic shift left"
  27.             on the double-long value with r in the upper half
  28.             and x in the lower half.  This operation is typically
  29.             expressible in only one or two assembly instructions.
  30.         (3) Unrolling this loop is probably not a bad idea.
  31.  
  32.     ALGORITHM
  33.         The calculations are the base-two analogue of the square
  34.         root algorithm we all learned in grammar school.  Since we're
  35.         in base 2, there is only one nontrivial trial multiplier.
  36.  
  37.         Notice that absolutely no multiplications or divisions are performed.
  38.         This means it'll be fast on a wide range of processors.
  39. */
  40.  
  41. struct int_sqrt {
  42.     unsigned sqrt,
  43.          frac;
  44. };
  45.  
  46. void usqrt(unsigned long x, struct int_sqrt *q)
  47. {
  48.     unsigned long a = 0L;            /* accumulator */
  49.     unsigned long r = 0L;                   /* remainder */
  50.     unsigned long e = 0L;                   /* trial product */
  51.  
  52.     int i;
  53.  
  54.     for (i = 0; i < BITSPERLONG; i++)    /* NOTE 1 */
  55.     {
  56.         r = (r << 2) + TOP2BITS(x); x <<= 2; /* NOTE 2 */
  57.         a <<= 1;
  58.         e = (a << 1) + 1;
  59.         if (r >= e)
  60.         {
  61.             r -= e;
  62.             a++;
  63.         }
  64.     }
  65.     memcpy(q, &a, sizeof(long));
  66. }
  67.  
  68. main()
  69. {
  70.     int i;
  71.     unsigned long l = 0x3fed0169;
  72.     struct int_sqrt q;
  73.  
  74.     for (i = 0; i < 101; ++i)
  75.     {
  76.         usqrt(i, &q);
  77.         printf("sqrt(%3d) = %2d, remainder = %2d\n",
  78.             i, q.sqrt, q. frac);
  79.     }
  80.     usqrt(l, &q);
  81.     printf("\nsqrt(%lX) = %X, remainder = %X\n", l, q.sqrt, q.frac);
  82. }
  83.